#include "TChart.h"
#include <QMainWindow>
#include <QColorDialog>
#include <QFile>
#include <QFileDialog>
#include <QTableWidget>

#include "img/pixOpen.xpm"
#include "img/pixTools.xpm"


//-------------------------------------

TChart::TChart( QWidget * parent ) :
    QFrame( parent ),
    grid( 0 ),
    leg( 0 ),
    mainPlot( 0 ),
    toolPalette( 0 ),
    wzoom( 0 ),
    mdl( 0 ),
    axScaler( 0 )
{
    resize( 1300, 700 );
    setWindowTitle( "Chart 2D" );
    curves.clear();

    glay = new QGridLayout( this );
    glay->setContentsMargins( 0, 0, 0, 0 );

    toolPalette = new QToolBar();
    toolPalette->setParent( this );
    glay->setMenuBar( toolPalette );
    toolPalette->addAction( QPixmap( pixOpen_xpm ), str8bit( TCHART_OPEN_CHART ) );
    toolPalette->addAction( QPixmap( pixTools_xpm ), str8bit( TCHART_TOOLS ) );
    connect( toolPalette, SIGNAL( actionTriggered(QAction*) ), this, SLOT( sltToolPaletteClick(QAction*) ) );

    leg = new QwtLegend();
    grid = new QwtPlotGrid();
    mainPlot = new MainPlot();

    glay->setContentsMargins( 0, 0, 0, 0 );
    splH = new QSplitter( this );
    splH->setMinimumSize( QSize( 200, 0 ) );
    //splH->setMaximumSize( QSize( 16777215, 16777215 ) );
    splH->setLayoutDirection( Qt::LeftToRight );
    //splH->setAutoFillBackground( false );
    splH->setOrientation( Qt::Vertical );
    listView = new QListView( splH );
    splH->addWidget( listView );
    wgtControls  = new QWidget( splH );
    glayControls = new QGridLayout( wgtControls );
    glayControls->setContentsMargins( 0, 0, 0, 0 );
    btnClr = new QPushButton( "Set Color", wgtControls );
    lblSplineDots = new QLabel( "Spline Dots Num", wgtControls );
    sbSplineDots = new QSpinBox( wgtControls );
    lblLineWidth = new QLabel( "Line Width", wgtControls );
    sbLineWidth = new QSpinBox( wgtControls );
    lblLineType = new QLabel( "Line Type", wgtControls );
    cbLineType = new QComboBox( wgtControls );
    btnDotsColor = new QPushButton( "Dot Color", wgtControls );
    lblDotsSize = new QLabel( "Dot Size", wgtControls );
    sbDotsSize = new QSpinBox( wgtControls );
    lblDotsType = new QLabel( "Dot Type", wgtControls );
    cbDotsType = new QComboBox( wgtControls );
    chbUseSpline = new QCheckBox( "Use Spline", wgtControls );
    btnBndTop = new QPushButton( "^", wgtControls );
    btnBndBottom = new QPushButton( "v", wgtControls );
    btnBndLeft = new QPushButton( "<", wgtControls );
    btnBndRight = new QPushButton( ">", wgtControls );
    dsbBndTop = new QDoubleSpinBox( wgtControls );
    dsbBndBottom = new QDoubleSpinBox( wgtControls );
    dsbBndLeft = new QDoubleSpinBox( wgtControls );
    dsbBndRight = new QDoubleSpinBox( wgtControls );
    cbXScaleType = new QComboBox( wgtControls );
    cbYScaleType = new QComboBox( wgtControls );
    lblXScaleType = new QLabel( "X Scale Type", wgtControls );
    lblYScaleType = new QLabel( "Y Scale Type", wgtControls );
    btnAdjustCrv = new QPushButton( "Adjust Axis to Curve", wgtControls );

    tabWgt = new QTabWidget( this );
    tabWgt->addTab( mainPlot, QString( "Charts" ) );

    glayControls->addWidget( btnClr,        0, 0, 1, 2 );
    glayControls->addWidget( lblSplineDots, 2, 0, 1, 1 );
    glayControls->addWidget( sbSplineDots,  2, 1, 1, 1 );
    glayControls->addWidget( lblLineWidth,  3, 0, 1, 1 );
    glayControls->addWidget( sbLineWidth,   3, 1, 1, 1 );
    glayControls->addWidget( lblLineType,   4, 0, 1, 1 );
    glayControls->addWidget( cbLineType,    4, 1, 1, 1 );
    glayControls->addWidget( btnDotsColor,  5, 0, 1, 2 );
    glayControls->addWidget( lblDotsSize,   6, 0, 1, 1 );
    glayControls->addWidget( sbDotsSize,    6, 1, 1, 1 );
    glayControls->addWidget( lblDotsType,   7, 0, 1, 1 );
    glayControls->addWidget( cbDotsType,    7, 1, 1, 1 );
    glayControls->addWidget( chbUseSpline,  1, 0, 1, 2 );
    glayControls->addWidget( btnAdjustCrv, 8, 0, 1, 2 );
    glayControls->addWidget( btnBndTop,     9, 0, 1, 1 );
    glayControls->addWidget( btnBndBottom,  10, 0, 1, 1 );
    glayControls->addWidget( btnBndLeft,    11, 0, 1, 1 );
    glayControls->addWidget( btnBndRight,   12, 0, 1, 1 );
    glayControls->addWidget( dsbBndTop,     9, 1, 1, 1 );
    glayControls->addWidget( dsbBndBottom,  10, 1, 1, 1 );
    glayControls->addWidget( dsbBndLeft,    11, 1, 1, 1 );
    glayControls->addWidget( dsbBndRight,   12, 1, 1, 1 );
    glayControls->addWidget( cbXScaleType, 13, 1, 1, 1 );
    glayControls->addWidget( cbYScaleType, 14, 1, 1, 1 );
    glayControls->addWidget( lblXScaleType, 13, 0, 1, 1 );
    glayControls->addWidget( lblYScaleType, 14, 0, 1, 1 );

    dsbBndTop->setLocale( QLocale::c() );
    dsbBndTop->setDecimals(4);
    dsbBndTop->setMinimum(-1e+09);
    dsbBndTop->setMaximum(1e+09);
    dsbBndTop->setSingleStep(0.0001);
    dsbBndTop->setValue(0);

    dsbBndBottom->setLocale( QLocale::c() );
    dsbBndBottom->setDecimals(4);
    dsbBndBottom->setMinimum(-1e+09);
    dsbBndBottom->setMaximum(1e+09);
    dsbBndBottom->setSingleStep(0.0001);
    dsbBndBottom->setValue(0);

    dsbBndLeft->setLocale( QLocale::c() );
    dsbBndLeft->setDecimals(4);
    dsbBndLeft->setMinimum(-1e+09);
    dsbBndLeft->setMaximum(1e+09);
    dsbBndLeft->setSingleStep(0.0001);
    dsbBndLeft->setValue(0);

    dsbBndRight->setLocale( QLocale::c() );
    dsbBndRight->setDecimals(4);
    dsbBndRight->setMinimum(-1e+09);
    dsbBndRight->setMaximum(1e+09);
    dsbBndRight->setSingleStep(0.0001);
    dsbBndRight->setValue(0);

    chbUseSpline->setChecked( true );

    splH->addWidget ( wgtControls );

    glay->addWidget( splH,     0, 0, 1, 1 );
    glay->addWidget( tabWgt, 0, 1, 1, 10 );

    mainPlot->setCanvasBackground( TCHART_DEFAULT_BGCOLOR );
    mainPlot->insertLegend( leg, QwtPlot::TopLegend );
    grid->attach( mainPlot );

    mainPlot->canvas()->setCursor( Qt::ArrowCursor );

    leg->setItemMode( QwtLegend::ReadOnlyItem );
    //leg->setItemMode( QwtLegend::CheckableItem );

    grid->enableXMin( true );
    grid->enableYMin( true );
    grid->setMajPen( QPen( TCHART_DEFAULT_GRID_COLOR, 0, Qt::DotLine ) );
    grid->setMinPen( QPen( TCHART_DEFAULT_GRID_COLOR, 0, Qt::DotLine ) );

    wzoom = new WheelZoom( mainPlot );
    axScaler = new TChartAxesScaler( mainPlot );

    cbLineType->addItem( "NoPen" );
    cbLineType->addItem( "SolidLine" );
    cbLineType->addItem( "DashLine" );
    cbLineType->addItem( "DotLine" );
    cbLineType->addItem( "DashDotLine" );
    cbLineType->addItem( "DashDotDotLine" );
    //cbLineType->addItem( "CustomDashLine" );
    cbLineType->setEditable( false );

    cbDotsType->addItem( "NoSymbol" );
    cbDotsType->addItem( "Ellipse" );
    cbDotsType->addItem( "Rect" );
    cbDotsType->addItem( "Diamond" );
    cbDotsType->addItem( "Triangle" );
    cbDotsType->addItem( "DTriangle" );
    cbDotsType->addItem( "UTriangle" );
    cbDotsType->addItem( "LTriangle" );
    cbDotsType->addItem( "RTriangle" );
    cbDotsType->addItem( "Cross" );
    cbDotsType->addItem( "XCross" );
    cbDotsType->addItem( "HLine" );
    cbDotsType->addItem( "VLine" );
    cbDotsType->addItem( "Star1" );
    cbDotsType->addItem( "Star2" );
    cbDotsType->addItem( "Hexagon" );
    cbDotsType->setEditable( false );

    cbXScaleType->addItem( "Linear" );
    cbXScaleType->addItem( "Logarithmic" );
    cbYScaleType->addItem( "Linear" );
    cbYScaleType->addItem( "Logarithmic" );

    sbLineWidth->setMinimum( 0 );
    sbLineWidth->setValue( 0 );

    sbDotsSize->setValue( 0 );
    sbSplineDots->setMinimum( 0 );

    sbSplineDots->setValue( 0 );
    sbSplineDots->setMinimum( 0 );
    sbSplineDots->setMaximum( 999999900 );
    sbSplineDots->setSingleStep( 100 );

    connect( listView, SIGNAL( clicked(QModelIndex)), this, SLOT( sltListviewClickItem(QModelIndex)) );
    connect( btnClr, SIGNAL( clicked()), this, SLOT( sltSetLineColor()) );
    connect( cbLineType, SIGNAL( currentIndexChanged(int)), this, SLOT( sltSetLineType()) );
    connect( sbLineWidth, SIGNAL( valueChanged(QString)), this, SLOT( sltSetWidth()) );
    connect( sbSplineDots, SIGNAL( valueChanged(QString)), this, SLOT( sltSetSplineDotsNum()) );
    connect( btnDotsColor, SIGNAL( clicked()), this, SLOT( sltSetDotsColor()) );
    connect( cbDotsType, SIGNAL( currentIndexChanged(int)), this, SLOT( sltSetDotsType()) );
    connect( sbDotsSize, SIGNAL( valueChanged(QString)), this, SLOT( sltSetDotsSize()) );
    connect( chbUseSpline, SIGNAL( clicked()), this, SLOT( sltUseSpline()) );
    connect( btnBndTop, SIGNAL( clicked()), this, SLOT( sltSetMaxY()) );
    connect( btnBndBottom, SIGNAL( clicked()), this, SLOT( sltSetMinY()) );
    connect( btnBndLeft, SIGNAL( clicked()), this, SLOT( sltSetMinX()) );
    connect( btnBndRight, SIGNAL( clicked()), this, SLOT( sltSetMaxX()) );

    dsbBndTop->setContextMenuPolicy( Qt::CustomContextMenu );
    dsbBndBottom->setContextMenuPolicy( Qt::CustomContextMenu );
    dsbBndLeft->setContextMenuPolicy( Qt::CustomContextMenu );
    dsbBndRight->setContextMenuPolicy( Qt::CustomContextMenu );
    connect( dsbBndTop, SIGNAL( customContextMenuRequested(QPoint)), this, SLOT( sltTopBndDsbRightClick()) );
    connect( dsbBndBottom, SIGNAL( customContextMenuRequested(QPoint)), this, SLOT( sltBottomBndDsbRightClick()) );
    connect( dsbBndLeft, SIGNAL( customContextMenuRequested(QPoint)), this, SLOT( sltLeftBndDsbRightClick()) );
    connect( dsbBndRight, SIGNAL( customContextMenuRequested(QPoint)), this, SLOT( sltRightBndDsbRightClick()) );

    connect( cbXScaleType, SIGNAL( currentIndexChanged(int)), this, SLOT( sltSetXScaleType()) );
    connect( cbYScaleType, SIGNAL( currentIndexChanged(int)), this, SLOT( sltSetYScaleType()) );

    connect( btnAdjustCrv, SIGNAL( clicked()), this, SLOT( sltAdjustAxis2Crv()) );

    renewCurvesList();
    setAttribute( Qt::WA_DeleteOnClose, true );
}

//-------------------------------------

TChart::~TChart()
{
    //while( curves.count() )
    //   delete curves.at( 0 );
    delete grid;
    grid = 0;
    delete leg;
    leg = 0;
    delete glay;
    glay = 0;
    delete toolPalette;
    toolPalette = 0;
    delete wzoom;
    wzoom = 0;
    delete axScaler;
    axScaler = 0;
    delete mainPlot;
    mainPlot = 0;
}

//------------------------------------

TCurve * TChart::CreateCurve( QVector< double > & x, QVector< double > & y, QString name )
{
    QPolygonF p;
    for( int i=0; i < x.count(); ++i )
    {
        p.append( QPointF( x.at( i ), y.at( i ) ) );
    }

    if( p.count() )
    {
        CreateCurve( p , name );
        renewCurvesList();
        addTableTab( name, p );
    }
}

//------------------------------------

void TChart::sltToolPaletteClick( QAction * a )
{
    if( a->text() == TCHART_OPEN_CHART )
    {
        QString fName = QFileDialog::getOpenFileName( this, "Open Chart Data File", "", "Double Pair Data File ( *.txt )" );

        if( fName.isEmpty() )
            return;

        QFile file( fName );
        QString fstr;

        if( file.open( QFile::ReadOnly | QIODevice::Text ) )
        {
            QTextStream out( &file );
            fstr = out.readAll();
            file.close();

            QStringList sl = fstr.split( "\n" );
            if( sl.count() )
            {
                QRegExp first = QRegExp( "^([ ]+)?(-?[0-9.,]+)" );
                QRegExp last = QRegExp( "(-?[0-9.,]+)$" );
                QString line;
                QPolygonF p;
                for( int i=0; i < sl.count(); ++i )
                {
                    line = sl.at( i );
                    if( first.indexIn( line ) > -1 && last.indexIn( line ) > -1 )
                        p.append( QPointF( first.cap( 0 ).toDouble(), last.cap( 0 ).toDouble() ) );
                }

                if( p.count() )
                {
                    CreateCurve( p , fName );
                    renewCurvesList();
                    addTableTab( fName, p );
                }
            }
        }
    }
    else if( a->text() == TCHART_TOOLS )
    {
        if( splH->isVisible() )
            splH->hide();
        else
            splH->show();
    }
}

//-------------------------------------

void TChart::renewCurvesList()
{
    if( mdl )
        delete mdl;

    QStringList sl;
    sl.append( TCHART_BACKGROUND );
    sl.append( TCHART_GRID );
    for( int i=0; i < curves.count(); ++i )
        sl.append( curves.at( i )->Title().text() );

    mdl = new QStringListModel( sl );
    listView->setModel( mdl );
    listView->selectionModel()->select( mdl->index( 0, 0 ), QItemSelectionModel::Select );
    sltListviewClickItem( mdl->index( 0, 0 ) );
}

//-------------------------------------

void TChart::createCurves()
{
    QPolygonF p1;
    QPolygonF p2;

    for( int i = -28; i < 28; ++i )
    {
        p1.append( QPointF( i, cos( 3*M_PI*i/7 ) ) );
        p2.append( QPointF( i, sin( M_PI*i/9 ) ) );
    }

    CreateCurve( p1, "c1" );
    CreateCurve( p2, "c2" );

    renewCurvesList();
}

//-------------------------------------

TCurve * TChart::CreateCurve( QVector< QPointF > & dots, QString name )
{
    smoothing( dots );
    TCurve * c = new TCurve( name );
    c->SetSamples( dots );
    c->Attach( mainPlot );
    c->SetPen( QPen( (uint)rand() ) );
    curves.append( c );
    mainPlot->replot();
    return c;
}

//-------------------------------------

void TChart::smoothing( QVector< QPointF > & dots )
{
    dots.append( dots.at( dots.count()-1 ) );
}

//-------------------------------------

void TChart::enableControls( QString & name )
{
    if( name == TCHART_BACKGROUND )
    {
        chbUseSpline->setEnabled( false );
        cbLineType->setEnabled( false );
        sbLineWidth->setEnabled( false );
        sbSplineDots->setEnabled( false );
        sbDotsSize->setEnabled( false );
        cbDotsType->setEnabled( false );
        btnDotsColor->setEnabled( false );
    }
    else if( name == TCHART_GRID )
    {
        chbUseSpline->setEnabled( false );
        cbLineType->setEnabled( true );
        sbLineWidth->setEnabled( true );
        sbSplineDots->setEnabled( false );
        sbDotsSize->setEnabled( false );
        cbDotsType->setEnabled( false );
        btnDotsColor->setEnabled( false );
    }
    else
    {
        chbUseSpline->setEnabled( true );
        cbLineType->setEnabled( true );
        sbLineWidth->setEnabled( true );
        sbSplineDots->setEnabled( true );
        sbDotsSize->setEnabled( true );
        cbDotsType->setEnabled( true );
        btnDotsColor->setEnabled( true );
    }
}

//-------------------------------------

TCurve * TChart::getCurveByName( QString & name )
{
    TCurve * crv = 0;
    for( int i=0; i < curves.count(); ++i )
    {
        crv = curves.at( i );
        if( crv->Title() == name )
            break;
        crv = 0;
    }
    return crv;
}

//-------------------------------------

TCurve * TChart::getSelectedCurve()
{
    QModelIndexList il = listView->selectionModel()->selectedIndexes();
    if( il.count() )
    {
        QString line = listView->model()->data( il.at( 0 ) ).toString();
        return getCurveByName( line );
    }
    return 0;
}

//-------------------------------------

void TChart::sltListviewClickItem( QModelIndex ind )
{
    QString line = listView->model()->data( ind ).toString();

    enableControls( line );

    TChartItem chit;

    if( line == TCHART_BACKGROUND )
    {
        chit.Color = mainPlot->canvasBackground().color();
    }
    else if( line == TCHART_GRID )
    {
        chit.Color = grid->majPen().color();
        chit.Width = grid->majPen().width();
        chit.LineType = grid->majPen().style();
    }
    else
    {
        TCurve * crv = getCurveByName( line );

        if( crv )
        {
            chit.Color = crv->Pen().color();
            chit.Width = crv->Pen().width();
            chit.LineType = crv->Pen().style();
            chit.SplineSize = crv->SplineSize();
            chit.DotColor = crv->DotColor();
            chit.DotSize = crv->DotSize();
            chit.DotType = crv->DotStyle();

            chbUseSpline->setChecked( crv->UseSpline() );
        }
    }

    btnClr->setPalette( QPalette( chit.Color ) );
    cbLineType->setCurrentIndex( chit.LineType );
    sbLineWidth->setValue( chit.Width );
    sbSplineDots->setValue( chit.SplineSize );
    sbDotsSize->setValue( chit.DotSize );
    cbDotsType->setCurrentIndex( chit.DotType+1 );
    btnDotsColor->setPalette( QPalette( chit.DotColor ) );
}

//-------------------------------------

void TChart::execControl( QObject * obj )
{
    if( obj == 0 )
        return;

    if( QPushButton * btn = dynamic_cast< QPushButton* >( obj ) )
    {
        double top;
        double bottom;
        double left;
        double right;
        QwtScaleMap sm = mainPlot->canvasMap( mainPlot->AxisX() );
        left = sm.s1();
        right = sm.s2();
        sm = mainPlot->canvasMap( mainPlot->AxisY() );
        bottom = sm.s1();
        top = sm.s2();

        if( btn == btnBndTop )
            mainPlot->setAxisScale( mainPlot->AxisY(), bottom, dsbBndTop->value() );
        else if( btn == btnBndBottom )
            mainPlot->setAxisScale( mainPlot->AxisY(), dsbBndBottom->value(), top );
        else if( btn == btnBndLeft )
            mainPlot->setAxisScale( mainPlot->AxisX(), dsbBndLeft->value(), right );
        else if( btn == btnBndRight )
            mainPlot->setAxisScale( mainPlot->AxisX(), left, dsbBndRight->value() );
    }
    else if( QDoubleSpinBox * dsb = dynamic_cast< QDoubleSpinBox* >( obj ) )
    {
        double top;
        double bottom;
        double left;
        double right;
        QwtScaleMap sm = mainPlot->canvasMap( mainPlot->AxisX() );
        left = sm.s1();
        right = sm.s2();
        sm = mainPlot->canvasMap( mainPlot->AxisY() );
        bottom = sm.s1();
        top = sm.s2();

        if( dsb == dsbBndTop )
            dsb->setValue( top );
        else if( dsb == dsbBndBottom )
            dsb->setValue( bottom );
        else if( dsb == dsbBndLeft )
            dsb->setValue( left );
        else if( dsb == dsbBndRight )
            dsb->setValue( right );
    }
    else if( QComboBox * cb = dynamic_cast< QComboBox* >( obj ) )
    {
        if( cb == cbXScaleType )
        {
            mainPlot->SetAxisScaleEngie( mainPlot->AxisX(), (MainPlot::ScEngType)cb->currentIndex() );
        }
        else if( cb == cbYScaleType )
        {
            mainPlot->SetAxisScaleEngie( mainPlot->AxisY(), (MainPlot::ScEngType)cb->currentIndex() );
        }
    }
    {
        TCurve * crv = getSelectedCurve();
        if( crv )
        {
            if( dynamic_cast< QPushButton* >( obj ) == btnClr )
            {
                QColor c = btnClr->palette().color( QPalette::Button );
                c = QColorDialog::getColor( c );
                crv->SetPen( QPen( c ) );
                btnClr->setPalette( QPalette( c ) );
            }
            else if( dynamic_cast< QPushButton* >( obj ) == btnDotsColor )
            {
                QColor c = btnDotsColor->palette().color( QPalette::Button );
                c = QColorDialog::getColor( c );
                crv->SetDotColor( c );
                btnDotsColor->setPalette( QPalette( c ) );
            }
            else if( dynamic_cast< QPushButton* >( obj ) == btnAdjustCrv )
            {
                QVector< QPointF > points = crv->Points();
                QVector< double > x;
                QVector< double > y;
                for( int i=0; i < points.count(); ++i )
                {
                    x.append( points.at( i ).x() );
                    y.append( points.at( i ).y() );
                }
                qSort( x );
                qSort( y );
                mainPlot->setAxisScale( mainPlot->AxisY(), y.at(0), y.at( crv->PointsCount()-1 ) );
                mainPlot->setAxisScale( mainPlot->AxisX(), x.at(0), x.at( crv->PointsCount()-1 ) );
            }
            else if( dynamic_cast< QSpinBox* >( obj ) == sbDotsSize )
            {
                crv->SetDotSize( sbDotsSize->value() );
            }
            else if( dynamic_cast< QSpinBox* >( obj ) == sbSplineDots )
            {
                int n = sbSplineDots->value();
                if( n < crv->PointsCount() )
                {
                    n = crv->PointsCount();
                    sbSplineDots->setValue( n );
                }
                crv->SetSplineSize( n );
            }
            else if( dynamic_cast< QSpinBox* >( obj ) == sbLineWidth )
            {
                QPen p = crv->Pen();
                p.setWidth( sbLineWidth->value() );
                crv->SetPen( p );
            }
            else if( dynamic_cast< QComboBox* >( obj ) == cbDotsType )
            {
                crv->SetDotStyle( (QwtSymbol::Style)( cbDotsType->currentIndex()-1 ) );
            }
            else if( dynamic_cast< QComboBox* >( obj ) == cbLineType )
            {
                QPen p = crv->Pen();
                p.setStyle( (Qt::PenStyle)cbLineType->currentIndex() );
                crv->SetPen( p );
            }
            else if( dynamic_cast< QCheckBox* >( obj ) == chbUseSpline )
            {
                crv->UseSpline( chbUseSpline->isChecked() );
            }
        }
        else
        {
            QModelIndexList il = listView->selectionModel()->selectedIndexes();
            if( il.count() )
            {
                QString line = listView->model()->data( il.at( 0 ) ).toString();
                if( line == TCHART_BACKGROUND )
                {
                    if( dynamic_cast< QPushButton* >( obj ) == btnClr )
                    {
                        QColor c = btnClr->palette().color( QPalette::Button );
                        c = QColorDialog::getColor( c );
                        btnClr->setPalette( QPalette( c ) );
                        mainPlot->setCanvasBackground( c );
                    }
                }
                else if( line == TCHART_GRID )
                {
                    if( dynamic_cast< QComboBox* >( obj ) == cbLineType )
                    {
                        QPen p = grid->majPen();
                        p.setStyle( (Qt::PenStyle)cbLineType->currentIndex() );
                        grid->setMajPen( p );
                        grid->setMinPen( p );
                    }
                    else if( dynamic_cast< QSpinBox* >( obj ) == sbLineWidth )
                    {
                        QPen p = grid->majPen();
                        p.setWidth( sbLineWidth->value() );
                        grid->setMajPen( p );
                        grid->setMinPen( p );
                    }
                    else if( dynamic_cast< QPushButton* >( obj ) == btnClr )
                    {
                        QColor c = btnClr->palette().color( QPalette::Button );
                        c = QColorDialog::getColor( c );
                        btnClr->setPalette( QPalette( c ) );
                        QPen p = grid->majPen();
                        p.setColor( c );
                        grid->setMajPen( p );
                        grid->setMinPen( p );
                    }
                }
            }
        }
    }
    mainPlot->replot();
}

//-------------------------------------

void TChart::addTableTab( QString name, QPolygonF & p )
{
    QTableWidget * tbl = new QTableWidget( p.count()-1, 2, this );
    QTableWidgetItem * twiX = 0;
    QTableWidgetItem * twiY = 0;
    QStringList headers;
    headers << "X" << "Y";
    tbl->setHorizontalHeaderLabels( headers );

    for( int i=0; i < p.count()-1; ++i )
    {
        twiX = new QTableWidgetItem( QString::number( p.at( i ).x() ) );
        twiY = new QTableWidgetItem( QString::number( p.at( i ).y() ) );
        tbl->setItem( i, 0, twiX );
        tbl->setItem( i, 1, twiY );
    }
    tabWgt->addTab( tbl, name );
}

//-------------------------------------















